package com.customization.huangwm.workflowNodeAction.SW003;

import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Author Huangwm
 * @Date 2022-08-01
 * @Version 1.0
 * @Description TODO 资产报失
 **/
public class AssetsLoss implements Action {
    private LogTool log = new LogTool("/log/SW003/huangwm/WfAction/AssetsLoss", false);
    private DBTools dbTools = new DBTools();

    public String type;//submit_create,submit_review,submit_end

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String nodeType = requestInfo.getRequestManager().getNodetype();
        String src = requestInfo.getRequestManager().getSrc();
        int creater = requestInfo.getRequestManager().getCreater();
        log.writeLog("資產報失流程>>>" + requestid + ">>" + nodeType + ">>" + src + ">>" + type);
        if (type.equals("") || (!type.equals("submit_create") && !type.equals("submit_review") && !type.equals("submit_end") && !type.equals("submit_reject"))) {
            requestInfo.getRequestManager().setMessageid(requestid);
            requestInfo.getRequestManager().setMessagecontent("未獲取到Action參數type或參數值不正確,請聯繫管理員檢查後台接口動作設置!;");
            return FAILURE_AND_CONTINUE;
        }

        RecordSet rs1 = new RecordSet();

        String tableName = requestInfo.getRequestManager().getBillTableName();
        String billid = requestInfo.getRequestManager().getBillid() + "";

        if (src.equals("reject")) {
            try {
                RecordSetTrans rst2 = new RecordSetTrans();
                rst2.setAutoCommit(false);
                //更新資產狀態為原狀態 1→oldStatus
                String sql1 = " update uf_003_AssetInfo2 set assetStatus=oldStatus from " + tableName + "_dt1 dt where dt.mainAssetCode=uf_003_AssetInfo2.id and mainid=" + billid;
                //更新物料申領記錄中的歸還中數量-1
                String sql2 = "update  uf_003_applicationRecord  set quantityInReturn=NVL(quantityInReturn,0)-1 from  "+tableName+"_dt1 dt "+
                        "\n where  uf_003_applicationRecord.mainAssetCode=dt.mainAssetCode and uf_003_applicationRecord.applicationUser="+creater+
                        "\n and mainid="+billid+" and (oper_staus is null or oper_staus=0)";
                if (rst2.getDBType().equals("oracle")) {
                    sql1 = "update uf_003_AssetInfo2 set assetStatus=(select oldStatus from " + tableName + "_dt1 where uf_003_AssetInfo2.id=mainAssetCode and mainid=" + billid + ")" +
                            "\n where id in (select mainAssetCode from " + tableName + "_dt1 where mainid=" + billid + ")";
                    sql2 = "update uf_003_applicationRecord  set quantityInReturn=NVL(quantityInReturn,0)-1\n" +
                            " where MAINASSETCODE in( select mainAssetCode  from  "+tableName+"_dt1 where mainid="+billid+") " +
                            " and (oper_staus is null or oper_staus=0)";
                }
                log.writeLog("退回更新資產狀態為原狀態SQL：" + sql1);
                log.writeLog("退回釋放物料申領記錄歸還中數量SQL："+sql2);
                boolean result1 = rst2.execute(sql1);
                boolean result2 = rst2.execute(sql1);
                if (!result2) {
                    rst2.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("退回釋放物料申領記錄歸還中數量SQL異常;" + sql2);
                    return FAILURE_AND_CONTINUE;
                }
                if (!result1) {
                    rst2.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("退回更新資產狀態為原狀態失敗,執行SQL異常;" + sql1);
                    return FAILURE_AND_CONTINUE;
                } else {
                    rst2.commit();
                    return SUCCESS;
                }
            } catch (Exception e) {
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("退回更新資產狀態為原狀態失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }
        } else if (type.equals("submit_create")) {
            try {
                //更新資產狀態為失中 7→G 先暂时用已报失去
                String sql1 = " update uf_003_AssetInfo2 set assetStatus='Missing' from " + tableName + "_dt1 dt where dt.mainAssetCode=uf_003_AssetInfo2.id and mainid=" + billid;
                //更新物料申領記錄中的歸還中數量+1
                String sql2 = "update  uf_003_applicationRecord  set quantityInReturn=NVL(quantityInReturn,0)+1 from  "+tableName+"_dt1 dt "+
                        "\n where  uf_003_applicationRecord.mainAssetCode=dt.mainAssetCode and uf_003_applicationRecord.applicationUser="+creater+
                        "\n and mainid="+billid+" and (oper_staus is null or oper_staus=0)";
                if (rs1.getDBType().equals("oracle")) {
                    sql1 = "update uf_003_AssetInfo2 set assetStatus=(select 'Missing' from " + tableName + "_dt1 dt where  uf_003_AssetInfo2.id=mainAssetCode and dt.mainid=" + billid + ")" +
                            "\n where id in(select mainAssetCode from  " + tableName + "_dt1 where mainid=" + billid + ")";
                    sql2 = "update uf_003_applicationRecord  set quantityInReturn=NVL(quantityInReturn,0)+1\n" +
                            " where MAINASSETCODE in( select mainAssetCode  from  "+tableName+"_dt1 where mainid="+billid+") " +
                            " and (oper_staus is null or oper_staus=0)";
                }
                log.writeLog("創建提交更新資產狀態為報失中SQL：" + sql1);
                log.writeLog("創建提交更新物料申領記錄歸還中數量SQL："+sql2);
                RecordSetTrans rst1 = new RecordSetTrans();
                rst1.setAutoCommit(false);
                boolean result1 = rst1.execute(sql1);
                boolean result2 = rst1.execute(sql2);
                if (!result2) {
                    rst1.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("創建提交更新物料申領記錄歸還中數量SQL異常;" + sql2);
                    return FAILURE_AND_CONTINUE;
                }
                if (!result1) {
                    rst1.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("創建提交更新資產狀態為報失中SQL異常;" + sql1);
                    return FAILURE_AND_CONTINUE;
                } else {
                    rst1.commit();
                    return SUCCESS;
                }
            } catch (Exception e) {
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("創建提交檢查/更新資產失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }
        } else if (type.equals("submit_end")) {
            try {
                RecordSetTrans rst1 = new RecordSetTrans();
                rst1.setAutoCommit(false);
                //更新資產表狀態為已報失 E
                String sql1 = " update uf_003_AssetInfo2 set assetStatus='Missing' from " + tableName + "_dt1 dt where dt.mainAssetCode=uf_003_AssetInfo2.id and dt.methodOfHanding=1  and mainid=" + billid;
                //更新物料申領中所關聯的資產記錄為已歸還 歸還中數量-1 已歸還數量+1
                String sql2 = "update uf_003_applicationRecord set quantityInReturn=NVL(quantityInReturn,0)-1,returnQuantity=NVL(returnQuantity,0)+1,oper_staus=1"+
                        "\n from "+tableName+"_dt1 dt where dt.mainAssetCode=uf_003_applicationRecord.mainAssetCode"+
                        "\n and uf_003_applicationRecord.applicationUser="+creater+" and mainid="+billid+" and (oper_staus is null or oper_staus=0)";
                if (rst1.getDBType().equals("oracle")) {
                    sql1 = "update uf_003_AssetInfo2 set assetStatus=(select 'Missing' from " + tableName + "_dt1 dt where  uf_003_AssetInfo2.id=mainAssetCode and dt.mainid=" + billid + ")" +
                            "\n where id in(select mainAssetCode from  " + tableName + "_dt1 where mainid=" + billid + ")";
                    sql2 = "update uf_003_applicationRecord  set quantityInReturn=NVL(quantityInReturn,0)-1,returnQuantity=NVL(returnQuantity,0)+1,oper_staus=1\n" +
                            " where MAINASSETCODE in( select mainAssetCode  from  "+tableName+"_dt1 where mainid="+billid+") " +
                            " and (oper_staus is null or oper_staus=0)";
                }
                log.writeLog("歸檔更新資產表狀態為已報失SQL：" + sql1);
                log.writeLog("歸檔更新物料申領中所關聯的資產記錄SQL："+sql2);

                boolean result1 = rst1.execute(sql1);
                boolean result2 = rst1.execute(sql2);
                log.writeLog("SQL執行結果;result1=" + result1+";result2="+result2);
                if (!result2) {
                    rst1.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("歸檔更新物料申領中所關聯的資產記錄SQL異常;" + sql2);
                    return FAILURE_AND_CONTINUE;
                }
                if (!result1) {
                    rst1.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("歸檔更新資產表狀態為已報失SQL異常;" + sql1);
                    return FAILURE_AND_CONTINUE;
                } else {
                    //先對接用友 再提交更新，對接失敗則rollback
                    String eop_reqUrl = new BaseBean().getPropValue("SW003", "eop_reqUrl");
                    if (eop_reqUrl.isEmpty()) {
                        eop_reqUrl = "https://testeop.macaowater.com";
                    }
                    String eop_reqToken = new BaseBean().getPropValue("SW003", "eop_reqToken");
                    if (eop_reqToken.isEmpty()) {
                        eop_reqToken = "EapUfida20200630D1207F4TEST";
                    }
                    rst1.commit();
                    //提交OA的庫存更新
                    return SUCCESS;
                }
            } catch (Exception e) {
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("歸檔更新資產為已報失失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }
        }


        return FAILURE_AND_CONTINUE;
    }
}
